﻿<Window x:Class="InvokeCommandActionWithEventArgs.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:InvokeCommandActionWithEventArgs"
        xmlns:oi="clr-namespace:Okazuki.MVVM.PrismSupport.Interactivity;assembly=Okazuki.MVVM.PrismSupport"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 
        xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" 
        xmlns:Custom="http://www.codeplex.com/prism" 
        WindowStartupLocation="CenterScreen"
        Title="Sample" Height="310" Width="400" ResizeMode="NoResize" FontSize="18">
    <Window.DataContext>
        <local:MainWindowViewModel>
            <local:MainWindowViewModel.People>
                <local:Person Name="太郎" Age="70"/>
                <local:Person Name="次郎" Age="60"/>
                <local:Person Name="三郎" Age="50"/>
                <local:Person Name="四郎" Age="40"/>
                <local:Person Name="正隆" Age="30"/>
                <local:Person Name="六郎" Age="20"/>
            </local:MainWindowViewModel.People>
        </local:MainWindowViewModel>
    </Window.DataContext>
    <i:Interaction.Triggers>
        <Custom:InteractionRequestTrigger SourceObject="{Binding AlertRequest, Mode=OneWay}">
            <oi:ShowMessageBoxAction/>
        </Custom:InteractionRequestTrigger>
    </i:Interaction.Triggers>
    <StackPanel>
        <ListBox x:Name="listBox1" HorizontalAlignment="Stretch" VerticalAlignment="Top" Height="170" 
                 ItemsSource="{Binding People}" SelectionMode="Multiple">
            <ListBox.Resources>
                <!--EventArgsを変換するためのIValueConverterの実装-->
                <local:SelectionChangedEventArgsToCommandParameterConverter x:Key="EventArgsConverter"/>
            </ListBox.Resources>
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="SelectionChanged">
                    <!--CommandParameterにIValueConverterの実装を指定します。-->
                    <oi:ExtendedInvokeCommandAction Command="{Binding CalcAverageCommand}" CommandParameter="{StaticResource EventArgsConverter}" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Name}" />
                        <TextBlock Text="{Binding Age, StringFormat='## さい'}" Margin="10,0,0,0"/>
                    </StackPanel>
                </DataTemplate>         
            </ListBox.ItemTemplate>       
        </ListBox>
        <TextBlock Margin="0,16,0,0" Text="{Binding Average, StringFormat='平均年齢は ##0.## さいです。'}" FontSize="24" />
        <Button Content="選択数表示" Height="32"  Width="auto" HorizontalAlignment="Left" Margin="0,4,0,0" >
            <Button.Resources>
                <local:CountConverter x:Key="CountConverter" />
            </Button.Resources>
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="Click">
                    <oi:ExtendedInvokeCommandAction 
                        Command="{Binding ShowCountCommand}"                                                  
                        CommandParameter="{Binding SelectedItems.Count, ElementName=listBox1, Converter={StaticResource CountConverter}}" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </Button>
    </StackPanel>
</Window>
